package com.sun.rabbit.did_queue;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.sun.rabbit.utils.RabbitMqUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;


/**生产者
 * @author shkstart
 * @create 2021-10-18 20:17
 */
public class Producer {
    private final static String EXCHANGE_NAME="normal_exchange";

    public static void main(String[] args) throws Exception {
        messageTTL02();
    }
    /*
    * 情况2.队列达到最大长度
    * */
    private static void messageTTL02() throws Exception{
        try(Channel channel = RabbitMqUtils.getChannel();){
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            //设置对列中消息的数量（// 该信息是用作演示队列个数限制）
            for (int i = 0; i <11 ; i++) {
                String message="info"+i;
                channel.basicPublish(EXCHANGE_NAME,"zhangsan",null,message.getBytes("UTF-8"));
                System.out.println("生产者发送消息:"+message);
            }
        }
    }

    /**
     *情况1.给消息设置生存时间TTL
     * @throws Exception
     */
    private static void messageTTL() throws Exception{
        try(Channel channel = RabbitMqUtils.getChannel();){
            //声明交换机和交换机的类型
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            //设置消息的过期时间TTL
            AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().expiration("10000").build();
           //设置对列中消息的数量（// 该信息是用作演示队列个数限制）
            for (int i = 0; i <11 ; i++) {
                String message="info"+i;
                channel.basicPublish(EXCHANGE_NAME,"zhangsan",properties,message.getBytes("UTF-8"));
                System.out.println("生产者发送消息:"+message);
            }
        }
    }
}
